library("mvtnorm")

source("~/R/MOG.R")
source("~/R/utils.R")
source("~/R/MOGshrinkage.R")

## have the following variables set:
## Dval
C1 <- 2;C2 <- 2;C3 <- 2
lambdaVal <- 0.0
theStandardizeFunction <- makeNoStandardizationF()

dpPerCluster = 10

## easy data set
easy.C1c1.mean <- c(-2,3)
easy.C1c1.var <- diag(c(0.1,0.1))
easy.C1c2.mean <- c(-2,5)
easy.C1c2.var <- diag(c(0.1,0.1))

easy.C2c1.mean <- c(4,4)
easy.C2c1.var <- diag(c(0.1,0.1))
easy.C2c2.mean <- c(4,5)
easy.C2c2.var <- diag(c(0.1,0.1))

easy.C3c1.mean <- c(-1,-1)
easy.C3c1.var <- diag(c(0.1,0.1))
easy.C3c2.mean <- c(-1,0)
easy.C3c2.var <- diag(c(0.1,0.1))

easy.X1 <- rbind(rmvnorm(dpPerCluster,easy.C1c1.mean,sqrt(easy.C1c1.var)),
                 rmvnorm(dpPerCluster,easy.C1c2.mean,sqrt(easy.C1c2.var)))
easy.X2 <- rbind(rmvnorm(dpPerCluster,easy.C2c1.mean,sqrt(easy.C2c1.var)),
                 rmvnorm(dpPerCluster,easy.C2c2.mean,sqrt(easy.C2c2.var)))
easy.X3 <- rbind(rmvnorm(dpPerCluster,easy.C3c1.mean,sqrt(easy.C3c1.var)),
                 rmvnorm(dpPerCluster,easy.C3c2.mean,sqrt(easy.C3c2.var)))

## hard data set
nasty.C1c1.mean <- c(-2,1.5)
nasty.C1c1.var <- diag(c(0.1,0.1))
nasty.C1c2.mean <- c(0,3)
nasty.C1c2.var <- diag(c(0.1,0.1))

nasty.C2c1.mean <- c(2,2)
nasty.C2c1.var <- diag(c(0.1,2.1))
nasty.C2c2.mean <- c(2,1)
nasty.C2c2.var <- diag(c(0.1,3.1))

nasty.C3c1.mean <- c(-1,0)
nasty.C3c1.var <- diag(c(0.1,1.1))
nasty.C3c2.mean <- c(-1,-1.5)
nasty.C3c2.var <- diag(c(0.1,1.1))

nasty.X1 <- rbind(rmvnorm(dpPerCluster,nasty.C1c1.mean,sqrt(nasty.C1c1.var)),
                  rmvnorm(dpPerCluster,nasty.C1c2.mean,sqrt(nasty.C1c2.var)))
nasty.X2 <- rbind(rmvnorm(dpPerCluster,nasty.C2c1.mean,sqrt(nasty.C2c1.var)),
                  rmvnorm(dpPerCluster,nasty.C2c2.mean,sqrt(nasty.C2c2.var)))
nasty.X3 <- rbind(rmvnorm(dpPerCluster,nasty.C3c1.mean,sqrt(nasty.C3c1.var)),
                  rmvnorm(dpPerCluster,nasty.C3c2.mean,sqrt(nasty.C3c2.var)))

Tstuff <- c(rep(1,times=20),rep(2,times=20),rep(3,times=20))


## create standardize function that will also project the data
## standardizeAndProjectF <- makeStandardizeAndPCAprojectF(t(trainX),Dval)
standardizeAndProjectF <- makeNoStandardizeF()

Pc <- c(1/3,1/3,1/3)

##### diagVar version #####
## learn
diagVar.X1 <- makeMoG(t(easy.X1),C=C1,
                      covRegF=makeShrinkageF("diagVar",lambdaVal),
                      standardizeF=standardizeAndProjectF)
diagVar.X2 <- makeMoG(t(easy.X2),C=C2,
                      covRegF=makeShrinkageF("diagVar",lambdaVal),
                      standardizeF=standardizeAndProjectF)
diagVar.X3 <- makeMoG(t(easy.X3),C=C3,
                      covRegF=makeShrinkageF("diagVar",lambdaVal),
                      standardizeF=standardizeAndProjectF)
diagVarGs <- makeMoG.supervised.allDesc(list(MoGmodels=list(diagVar.X1$MoGmodel,
                                               diagVar.X2$MoGmodel,
                                               diagVar.X3$MoGmodel),
                                             Pc=Pc),
                                        standardizeAndProjectF)
## classify
classifications.train <- useDiscFunc(diagVarGs,t(rbind(easy.X1,easy.X2,easy.X3)))
## evaluate
diagVar.classificationsAcc.train <- classificationAccuracy(classifications.train,Tstuff)
